//author:kakake
class tsregisterDao {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    constructor(workid: number) {
        this.data.WorkId = workid;
    }

    //获取科室分类和科室
    getdept() {
        // var strsql = "SELECT *  FROM WPMT_BaseDeptLayer where workid=@workid ";
        // var strsql2 = "SELECT *  FROM WPMT_BaseDept a   where a.workid=@workid ";

        var strsql = "SELECT *  FROM WPMT_BaseDeptLayer ";
        var strsql2 = "SELECT *  FROM WPMT_BaseDept a ";

        var layer = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { workid: this.data.WorkId });
        var dept = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2, { workid: this.data.WorkId });

        return { layer: layer, dept: dept };
    }

    //预约排班
    //显示医生以及医生各个时段的号源
    //schedtype 0 线下预约挂号 1线上互联网问诊
    getdocOrderSched(schedtype: any, _deptId: any, _empId?: any) {
        var deptId = _deptId || 0;
        var empId = _empId || 0;
        //a.workid=b.workid and
        //a.workid=c.workid and
        var strsql = "SELECT a.DeptID DeptId,a.DoctID DoctId,0 GoodNum,0 RegNum,isnull(b.Name,'') DeptName,isnull(c.EmpName,'') DocName FROM WPMT_DocSchedual a \
                        left join wpmt_basedept b on  a.DeptID=b.DeptId \
                        left join wpmt_doctor c on  a.DoctID=c.EmpId \
                        where a.SchedType=@schedtype and (a.DeptID=@deptId or 0>=@deptId) and (a.DoctID=@empId or 0>=@empId) AND \
                        SchedualDate>=CONVERT(varchar(10), GETDATE(), 120) AND \
                        SchedualDate<=CONVERT(varchar(10), DATEADD(DAY,6,GETDATE()),120) \
                        GROUP BY a.DeptID,a.DoctID,b.Name,c.EmpName";
        var dbparams = {
            schedtype: schedtype,
            deptId: deptId,
            empId: empId
        }
        var Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);

        //a.workid=b.workid and
        //a.workid=c.workid and
        Items.forEach(i => {
            strsql = "SELECT a.SchedualID RegId \
                        ,a.DeptID DeptId \
                        ,a.DoctID DoctId \
                        ,isnull(b.Name,'') DeptName \
                        ,isnull(c.EmpName,'') DocName \
                        ,CONVERT(varchar(10), SchedualDate, 120) RegDate \
                        ,Datepart(weekday, SchedualDate + @@DateFirst - 1) Week \
                        ,c.RegPrice RegFee \
                        ,a.TimeRange TimeType  \
                        ,a.AppointNum AllCount \
                        ,(a.AppointNum-(select COUNT(1) from WPMT_RegisteredPayment t where t.Regid=a.SchedualID and t.RegState=2)) HaveCount \
                        ,a.Flag scheduleStatus \
                        FROM WPMT_DocSchedual a \
                        left join wpmt_basedept b on  a.DeptID=b.DeptId \
                        left join wpmt_doctor c on  a.DoctID=c.EmpId \
                        where a.SchedType=@schedtype and a.DeptID=@DeptId and a.DoctID=@DoctId AND \
                        SchedualDate>=CONVERT(varchar(10), GETDATE(), 120) \
                        AND SchedualDate<=CONVERT(varchar(10), DATEADD(DAY,6,GETDATE()),120) order by a.SchedualDate,a.TimeRange";
            var dbparams2 = {
                schedtype: schedtype,
                DeptId: i.DeptId,
                DoctId: i.DoctId
            }
            i.scheduleStatus = 0;
            i.ItemA = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams2);
        });

        return Items;
    }

    //排班时间段
    scheduleTimeRange(_regid) {

        //1.查询指定号源的时段
        var strsql = "SELECT TOP 1 \
                        TimeRange Timeid \
                        ,case TimeRange WHEN 1 then '8:30' when 2 then '14:00'  else '18:00' end StartTime \
                        ,case TimeRange WHEN 1 then '12:00' when 2 then '18:00' else '23:00' end EndTime \
                        ,AppointNum AllCount \
                        ,(a.AppointNum-(select COUNT(1) from WPMT_RegisteredPayment t where t.Regid=a.SchedualID and t.RegState=2)) HaveCount \
                        FROM WPMT_DocSchedual a where SchedualID=@regid";
        var dbparams = {
            regid: _regid
        }
        var Items = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams);
        return Items;
    }

    /**
     * 获取医生列表
     */
    public getDoctorList(deptId?: number) {
        deptId = deptId || 0;
        // let strsql = 'SELECT * FROM WPMT_Doctor where WorkId=@workid';
        let strsql = 'SELECT * FROM WPMT_Doctor where 1=1 ';
        if (deptId > 0) {
            strsql += ' and DeptId=@deptId'
        }
        let data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { workid: this.data.WorkId, deptId: deptId });
        return data;
    }

    /**
     * 获取医生列表，分页
     */
    public getDoctorPageList(pageInfo, search) {

        // var strsql = "SELECT * FROM WPMT_Doctor where WorkId=@workid and EmpName like @search";
        var strsql = "SELECT * FROM WPMT_Doctor where EmpName like @search";
        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { workid: this.data.WorkId, search: '%' + search + '%' }, pageInfo);
        return { list: data, count: pageInfo.totalRecord };
    }

    /**
     * 医生详情
     */
    public getDoctorInfo(empId: number) {
        // let strsql = "select * from WPMT_Doctor where WorkId=@workid and EmpId=@empId";
        let strsql = "select * from WPMT_Doctor where EmpId=@empId";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { empId: empId, workid: this.data.WorkId });
    }

    /**
     * 挂号记录
     */
    public getregrecordlist(userId, patientId, pageInfo) {

        var dbparams = {
            workid: this.data.WorkId,
            userId: userId,
            PatientId: patientId
        }
        var strsql = "select r.*, isnull(d.HeadImg,'') UPic from WPMT_RegisteredPayment r left join WPMT_Doctor d on r.EmpId = d.EmpId \
        where r.RegType=1 and r.RegState in (2,4) and r.WorkId=@workid and MPUserID=@userId ";

        //RegState 1 预约中 2 预约成功 3 预约失败 4预约取消
        //OrderState 订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、

        if (parseInt(patientId) > 0) {
            strsql += " and r.PatientId=@PatientId"
        }

        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
        return { list: data, count: pageInfo.totalRecord };
    }

    /**
     * 问诊记录
     * status 0问诊中 1已完成
     */
    public getmedrecordlist(status, userId, patientId, pageInfo) {

        var dbparams = {
            workid: this.data.WorkId,
            userId: userId,
            PatientId: patientId
        }
        var strsql = "select r.*, isnull(d.HeadImg,'') UPic from WPMT_RegisteredPayment r left join WPMT_Doctor d on r.EmpId = d.EmpId \
        where r.RegType=2 and r.WorkId=@workid and MPUserID=@userId ";

        //RegState 1 预约中 2 预约成功 3 预约失败 4预约取消
        //OrderState 订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、

        if (parseInt(patientId) > 0) {
            strsql += " and r.PatientId=@PatientId"
        }

        if (parseInt(status) == 0) {
            strsql += " and r.RegState=2 ";
        }
        else if (parseInt(status) == 1) {
            strsql += " and r.RegState>2 ";
        }

        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
        return { list: data, count: pageInfo.totalRecord };
    }

    /**
     * 挂号记录详情
     */
    public getRegisteredInfo(id: number) {
        let strsql = "select * from WPMT_RegisteredPayment where Id=@id";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { id: id });
    }

    /**
     * 后台查询挂号记录
     */
    public getregrecordlist_web(sdate, edate, status, keyword, pageInfo) {

        var dbparams = {
            workid: this.data.WorkId,
            sdate: sdate,
            edate: edate,
            status: status,//0全部 1待支付 2已挂号 3已退款 4已取消
            keyword: "%" + keyword + "%"
        }

        //RegState 1 预约中 2 预约成功 3 预约失败 4预约取消
        //OrderState 订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、
        var strsql = "select * from WPMT_RegisteredPayment  where WorkId=@workid and PatName like @keyword and RegDate BETWEEN @sdate and @edate";
        if (parseInt(status) > 0) {
            if (parseInt(status) == 1) {
                strsql += ' and OrderState=1';
            }
            else if (parseInt(status) == 2) {
                strsql += ' and OrderState=2';
            }
            else if (parseInt(status) == 3) {
                strsql += ' and OrderState=7';
            }
            else if (parseInt(status) == 4) {
                strsql += ' and OrderState=5';
            }
        }

        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
        return { list: data, count: pageInfo.totalRecord };
    }

    //更新已支付状态
    public updateRegPayed(Id: number, RegNo: string, Location: string, TranSerNo: string, OrderNo: string, PayMode: number) {

        var strsql = " update WPMT_RegisteredPayment set RegState=2,OrderState=2,OrderNo=@OrderNo,PayTime=@PayTime,PayMode=@PayMode,TranSerNo=@TranSerNo,Waitno=@Waitno,RegNo=@RegNo,PatId=@PatId,Location=@Location where Id=@Id";
        var dbparams = {
            Id: Id,
            RegNo: RegNo,
            Location: Location,
            TranSerNo: TranSerNo,
            OrderNo: OrderNo,
            PayTime: TSLobsterAPI.Utils.getCurrTime(null),
            PayMode: PayMode,
            Waitno: '',
            PatId: RegNo
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    }

    /**
     * 退款中
     */
    public cancelregister(id: number) {
        //RegState 1 预约中 2 预约成功 3 预约失败 4预约取消
        //OrderState 订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、
        let strsql = " update WPMT_RegisteredPayment set OrderState=6 where Id=@id";
        var dbparams = {
            id: id
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    }

    /**
     * 退款完成
     */
    public cancelregisterfinshed(id: number, RefundNo: any, RefundTransactionId: any) {
        //RegState 1 预约中 2 预约成功 3 预约失败 4预约取消
        //OrderState 订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、
        let strsql = " update WPMT_RegisteredPayment set RegState=4,GetNumberState=9,OrderState=7,OrderNo_Refund=@RefundNo,TranSerNo_Refund=@RefundTransactionId where Id=@id";
        var dbparams = {
            id: id,
            RefundNo: RefundNo,
            RefundTransactionId: RefundTransactionId
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    }

    /**
    * 医生信息界面，显示点赞最多的评价
    */
    public getevaluationtopone(doctId: any) {
        let strsql = "select top 1 * from WPMT_DoctorEvaluation where workid=@workid and status=1 and EmpId=@doctId order by givecount desc";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { workid: this.data.WorkId, doctId: doctId });
    }


    /**
     * 更新评价点赞数
     */
    updateevaluationgivecount(id: any) {
        let strsql = "update WPMT_DoctorEvaluation set givecount=isnull(givecount,0)+1 where ID=@id";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { id: id });
    }
}